/*
*
*/
function formatobj(obj){
    let str = "";
    for( key in obj){
        str+="&"+key+"="+obj[key]
    }
    str = str.slice(1)
    return str;
}

const http = {

    // get请求
    get:function(url,date,callback){
        let xhr = new XMLHttpRequest();
        xhr.open("get",url+"?"+formatobj(date))
        xhr.send()
        xhr.onreadystatechange = function(){
 
          if(xhr.readyState ==4 && xhr.status ==200){
              let data =JSON.parse( xhr.responseText);
              callback(data);
          }
      }
    },
    // post请求
    post:function(url , date , callback){
        let xhr = new XMLHttpRequest();
        xhr.open("post",url)
        xhr.setRequestHeader("content-type","application/JSON")
        xhr.send(JSON.stringify(date))
        xhr.onreadystatechange = function(){
 
          if(xhr.readyState ==4 && xhr.status ==200){
              let data =JSON.parse( xhr.responseText);
              callback(data);
          }
      }
    },
    // ajax请求
    ajax:function(obj){
        if(obj.type == "get"){
            this.get(obj.url,obj.data,obj.success)
        }
        if(obj.type == "post"){
            this.post(obj.url,obj.data,obj.success)
        }
    }


}
window.http = http;